#include <nds.h>
#include <nds/fifocommon.h>
#include <maxmod9.h>

#include "../Map.h"
#include "../Game.h"
#include "Renderer.h"
#include "Mods.h"

extern Map level1;
extern uint8 Mods_msl[];

int main()
{
	powerON(POWER_ALL);
	irqInit();
	irqEnable(IRQ_VBLANK);
	fifoInit();

	Renderer renderer;
	InitRenderer(&renderer);

	PrepareForMap(&renderer,&level1);

	Game game;
	StartGame(&game,&level1);

	mmInitDefaultMem((mm_addr)Mods_msl);
	mmLoad(MOD_STARDUSTMEMORIES);
	mmStart(MOD_STARDUSTMEMORIES,MM_PLAY_LOOP);

	int frame=0;
	int row=0;
	for(;;)
	{
		scanKeys();
		uint16 keys=keysHeld();

		if(keys&KEY_START) RestartGame(&game,&level1);

		GameLoop(&game,&level1,keys&KEY_LEFT,keys&KEY_RIGHT,keys&KEY_UP,keys&KEY_DOWN,
		keys&(KEY_B|KEY_R),keys&(KEY_A|KEY_L));
		//nextloop+=1.0/25.0;

		while(fifoCheckValue32(12)) row=fifoGetValue32(12);

		Render(&renderer,&level1,&game,row>>1);

		swiWaitForVBlank();
		swiWaitForVBlank();
		if(frame%5==1||frame%5==3) swiWaitForVBlank();
		frame++;
	}

	return 0;
}
